﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public abstract class Bullet : GameEntity
    {
        protected IDTank _idTank;
        public IDTank IdTank
        {
            get { return _idTank; }
            set { _idTank = value; }
        }
        
        protected IDBullet _idBullet;
        public IDBullet IdBullet
        {
            get { return _idBullet; }
            set { _idBullet = value; }
        }

        public int Damages;

        protected Texture2D _image;
        public Texture2D Image
        {
            get { return _image; }
            set { _image = value; }
        }

        int _totalDamage;
        public int TotalDamage
        {
            get { return _totalDamage; }
            set { _totalDamage = value; }
        }

        protected BulletModel _model;
        public BulletModel Model
        {
            get { return _model; }
            set { _model = value; }
        }

        public StateInUse StateUsed;
        public int LifePower;

        public void Update(GameTime gameTime)
        {
            if (this.StateUsed == StateInUse.Boomimg)
            {
                this.StateUsed = StateInUse.NotUsed;
                return;
            }

            if (this.StateUsed == StateInUse.ReadyToboom)
            {
                this.StateUsed = StateInUse.Boomimg;
                return;
            }

            if (this.LifePower > 0)
            {
                this.StateUsed = StateInUse.Using;
            }
            else
            {
                this.StateUsed = StateInUse.ReadyToboom;
                return;
            }
            if (CheckCollisionMapBorder(this.RectCollisionOutside) == true)
            {
                this.LifePower = 0;
            }
            else
            {
                CheckCollision();
                this.Move();                                 
            }
            
        }

        public void CheckCollision()    // Kiểm tra các va chạm
        {
            
            CheckTerrainCollision();
            CheckTankCollision();
            CheckBulletCollision();
        }

        public void CheckTankCollision()    // Va chạm đạn - tank
        {
            if (!GlobalVariables.IsPlayerTankID(this.IdTank))   // Đạn của com tank
            {
                if (GlobalVariables.CheckRectCollision(this.RectCollisionOutside, UserControlledTankManager._tankLevel.RectCollisionOutside) == true)
                {
                    this.LifePower -= 1;
                    UserControlledTankManager._tankLevel.TankIsShooted(this);
                    return;
                }
            }
            else   // Đạn của Player Tank trúng Com tank
            {
                for (int i = 0; i < AutomatedTankManager._listComTanks.Count(); i++)
                {
                    if (GlobalVariables.CheckRectCollision(this.RectCollisionOutside, AutomatedTankManager._listComTanks.ElementAt(i).RectCollisionOutside)==true)
                    {
                        this.LifePower -= 1;
                        AutomatedTankManager._listComTanks.ElementAt(i).TankIsShooted(this);
                          
                        if (AutomatedTankManager._listComTanks.ElementAt(i).CurrentHP <= 0)
                        {
                            UserControlledTankManager._tankLevel.AddExperiencePoint(AutomatedTankManager._listComTanks.ElementAt(i).ExpMark);                           
                        }
                    }
                }

                return;
            }
        }

        public void CheckTerrainCollision() // Va chạm đạn - địa hình
        {
            TerrainItem terrainItem;
            
            for (int i = 0; i <  TerrainItemManager.terrainItems.Count(); i++)
            {
                terrainItem =  TerrainItemManager.terrainItems.ElementAt(i);
                if (GlobalVariables.CheckRectCollision(this.RectCollisionOutside, terrainItem.CollisionRect) == true)
                {
                    if (terrainItem._idItem == 100)  // Dan ban trung thanh
                    {

                    }

                    if (terrainItem._idItem != 3)    // Dia hinh khong fai la co - cay coi
                    {
                        this.LifePower -= 1;

                        //if(terrainItem._idItem ) // Dia hinh khong phai la be tong
                        {
                            TerrainItemManager.terrainItems.ElementAt(i).LifePower -= 1;
                            if ((this.IdTank == IDTank.idPlayerTankLevel1 || this.IdTank == IDTank.idPlayerTankLevel2 ||
                                this.IdTank == IDTank.idPlayerTankLevel3)                               
                                && GlobalVariables.HaveSoundEffect == true &&
                                TerrainItemManager.terrainItems.ElementAt(i).LifePower > 0)
                            {
                                GameResourceContainer.SoundManager.DestBrick.Play();
                            }

                        }
                        /*
                        else
                        {
                            if (this.IdTank == IDTank.idTank1 || this.IdTank == IDTank.idTank2 || this.IdTank == IDTank.idTank4)   // Xet ID cua dan
                            {
                                MyMap.terrainItems.ElementAt(i).LifePower -=1;
                            }
                        }*/
                    }
                }
            }
        }

        public void CheckBulletCollision()  // Va chạm đạn - đạn
        {
            if(GlobalVariables.IsPlayerTankID(this.IdTank)==false)   // Đạn của ComTank
            {
                for (int i = 0; i < UserControlledTankManager._tankLevel._bulletManager._listBullets.Count(); i++)
                {
                    if (GlobalVariables.CheckRectCollision(this.RectCollisionOutside,
                        UserControlledTankManager._tankLevel._bulletManager._listBullets.ElementAt(i).RectCollisionOutside) == true)
                    {
                        this.LifePower -= 1;
                        UserControlledTankManager._tankLevel._bulletManager._listBullets.ElementAt(i).LifePower -= 1;
                        return;
                    }
                }
            }
        }

        public override void Move()
        {
            if (this.IdTank == IDTank.idPlayerTankLevel1 || this.IdTank == IDTank.idPlayerTankLevel2 ||
               this.IdTank == IDTank.idPlayerTankLevel3)
            {               
                this.DirectionMoving = new Vector2((float)Math.Sin(RotationAngle), -(float)Math.Cos(RotationAngle));
                this.VectorSpeedMoving = new Vector2(this.ISpeed * 90, this.ISpeed * 90);
               
                this.Position += this.VectorSpeedMoving * this.DirectionMoving * 0.01f;            
            }

            else
            {
                base.Move();
            }
        }

        public override void  DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Image, _position, _size, Color.White,
               _rotationAngle, Origin, 1.0f, SpriteEffects.None, 0.0f);       
        }
    }
}
